package cn.net.autocode.platform.dingTalk;

import cn.hutool.core.util.DesensitizedUtil;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import cn.net.autocode.platform.auth.service.PlatformAuthServ;
import cn.net.autocode.platform.dingTalk.service.DingTalkServ;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/platform/dingTalk")
@RestController("platformDingTalkCtrl")
public class DingTalkCtrl {
    private final DingTalkServ dingTalkServ;
    private final PlatformAuthServ authServ;
    private final Dao dao;

    public DingTalkCtrl(DingTalkServ dingTalkServ, PlatformAuthServ authServ, Dao dao) {
        this.dingTalkServ = dingTalkServ;
        this.authServ = authServ;
        this.dao = dao;
    }

    @RequestMapping("detail")
    public ResultT detail(){
        JSONObject json = dao.queryJSON("select * from dingtalk_config");
        if (json != null && !json.isEmpty()) {
            String appId = json.getString("appId");
            json.put("appId",this.tuomin(appId));

            String clientId = json.getString("clientId");
            json.put("clientId",this.tuomin(clientId));

            String clientSecret = json.getString("clientSecret");
            json.put("clientSecret",this.tuomin(clientSecret));

            String corpId = json.getString("corpId");
            json.put("corpId",this.tuomin(corpId));

            String robotCode = json.getString("robotCode");
            json.put("robotCode",this.tuomin(robotCode));
        }
        return ResultT.success(json);
    }

    private String tuomin(String source){
        if (source != null && !source.isBlank()) {
            if (source.length() >8) {
                int tmNum = source.length()/3;
                return source.substring(0, tmNum)+"*".repeat(tmNum)+source.substring(tmNum*2);
            }else{
                return DesensitizedUtil.passport(source);
            }
        }else{
            return source;
        }
    }

    @RequestMapping("saveConfig")
    public ResultT saveConfig(@RequestBody JSONObject param){
        String id = param.getString("id");
        if (id == null || id.isBlank()){
            id = IdWorker.ulid();
            param.put("id", id);
            dao.createJSON(param,"dingtalk_config");
        }else{
            JSONObject json = new JSONObject();
            json.put("id", id);
            String appId = param.getString("appId");
            if (appId != null && !appId.isBlank()) {
                json.put("appId",appId);
            }

            String clientId = param.getString("clientId");
            if (clientId != null && !clientId.isBlank()) {
                json.put("clientId",clientId);
            }

            String clientSecret = param.getString("clientSecret");
            if (clientSecret != null && !clientSecret.isBlank()) {
                json.put("clientSecret",clientSecret);
            }

            String corpId = param.getString("corpId");
            if (corpId != null && !corpId.isBlank()) {
                json.put("corpId",corpId);
            }

            String robotCode = param.getString("robotCode");
            if (robotCode != null && !robotCode.isBlank()) {
                json.put("robotCode",robotCode);
            }

            if (json.size()>1){
                dao.updateJSON(json,"dingtalk_config");
            }
        }
        dingTalkServ.clean();
        return this.detail();
    }

    @RequestMapping("getCorpId")
    public ResultT getCorpId() {
        String corpId = dingTalkServ.getCorpId();
        JSONObject result = new JSONObject();
        result.put("corpId", corpId);
        return ResultT.success(result);
    }

    @RequestMapping("login")
    public ResultT authLogin(@RequestBody JSONObject param) {
        String code = param.getString("code");
        //获取accessToken
        String accessToken = dingTalkServ.getAccessToken();
        String userid = dingTalkServ.getUserIdByCode(accessToken,code);
        JSONObject userInfo = dingTalkServ.getUserInfo(accessToken,userid);
        String mobile = userInfo.getString("mobile");
        JSONObject json = authServ.ssoMobile(mobile,"Y");
        return ResultT.success(json);
    }

}
